// web/src/lib/mail.ts

// 国内防火墙
// import {Resend} from 'resend'
//
// const resend = new Resend(process.env.RESEND_API_KEY)
//
// export const sendVerificationEmail = async (email: string, token: string) => {
//     const confirmLink = `http://localhost:3000/auth/new-verification?token=${token}`
//     await resend.emails.send({
//         // from: 'onboarding@resend.dev',
//         from: 'Acme <onboarding@resend.dev>',
//         to: email,
//         subject: "Confirm your email",
//         html: `<p>Click <a href="${confirmLink}">here</a> to confirm email.</p>`
//     })
// }

// 替代
// @ts-ignore
import nodemailer from 'nodemailer'


let transporter = nodemailer.createTransport({
    host: 'smtp.163.com',
    secureConnection: true,
    port: 465,
    secure: true,
    auth: {
        user: "malguy2022@163.com",
        pass: process.env.EMAIL_163_PASS
    }
})

export const sendTwoFactorTokenEmail = async (email: string, token: string) => {
    let mailOptions = {
        from: 'malguy2022@163.com',
        to: email,
        subject: "2FA Code",
        html: `<p>Your 2FA code: ${token}</p>`,
    }

    // @ts-ignore
    transporter.sendMail(mailOptions, (err, info) => {
        if (err) console.error(err)
        else console.log(info)
    })
}

export const sendVerificationEmail = (email: string, token: string) => {
    const confirmLink = `http://localhost:3000/auth/new-verification?token=${token}`

    let mailOptions = {
        from: 'malguy2022@163.com',
        to: email,
        subject: "Confirm your email",
        html: `<p>Click <a href="${confirmLink}">here</a> to confirm email.</p>`,
        // attachments: [{
        //     filename:'',
        //     path:'./'
        // }]
    }

    // @ts-ignore
    transporter.sendMail(mailOptions, (err, info) => {
        if (err) console.error(err)
        else console.log(info)
    })
}

export const sendPasswordResetEmail = (email: string, token: string) => {
    const resetLink = `http://localhost:3000/auth/new-password?token=${token}`

    let mailOptions = {
        from: 'malguy2022@163.com',
        to: email,
        subject: "Reset your password",
        html: `<p>Click <a href="${resetLink}">here</a> to reset password.</p>`
    }

    // @ts-ignore
    transporter.sendMail(mailOptions, (err, info) => {
        if (err) console.error(err)
        else console.log(info)
    })
}